package com.apes.fn.server.servicePlan.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.fn.server.servicePlan.model.GetPriceLogger;
import com.apes.fn.server.servicePlan.repository.GetPriceLoggerRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

/**
 * @program: apes-server
 * @description:
 * @author: czs
 * @create: 2022-11-11 14:22
 **/
@Service("getPriceLoggerService")
public class GetPriceLoggerService extends DomainService {

    @Autowired
    private GetPriceLoggerRepository getPriceLoggerRepository;

    private GetPriceLogger findById(SimpleRequest request){
        JSONObject jo = request.getJO();
        String id = jo.getString("id");
        if (id==null)return null;
        GetPriceLogger priceLogger = getPriceLoggerRepository.findOne(id);
        return priceLogger;
    }

    public void addInquiryExtendDetail(JSONObject jo, JSONArray result, boolean isLoss){
        if (isLoss)return;
        JSONArray spxjdmx = jo.getJSONArray("PUR_SPXJDMX");
        if (spxjdmx==null)return;
        if (result==null)return;
        for (Object o : result) {
            JSONObject jsonObject = (JSONObject) o;
            String priceSource = jsonObject.getString("priceSource");
            GetPriceLogger priceLogger = getPriceLoggerRepository.findOne(priceSource);
            if (priceLogger==null)return;
            JSONObject spxjdJo = new JSONObject(Objects.requireNonNull(MapUtil.mapped(spxjdmx.get(0))));
            JSONObject condition = new JSONObject();
            condition.put("isLoss",isLoss);
            condition.put("spxjdJo",spxjdJo);
            priceLogger.setConditionParam(condition.toJSONString());
            getPriceLoggerRepository.saveAndFlush(priceLogger);
        }
    }

    /**
     * 清理无效数据
     * delete from GET_PRICE_LOGGER a
     * where not exists(select 1 from SER_SERVICE_PLAN_ITEM b where a.id = b.PRICE_SOURCE)
     * and CREATE_DATE <SYSDATE-1;
     *
     */
}
